package com.zhangjianling.premp.server.sys.impl;

import com.zhangjianling.premp.dao.sys.SysUserDao;
import com.zhangjianling.premp.entity.sys.SysUser;
import com.zhangjianling.premp.server.sys.SysUserService;
import com.zhangjianling.premp.utils.SystemAopLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;


@Service
@Transactional
public class SysUserServiceImpl implements SysUserService {

	@Resource
	private SysUserDao sysUserDao;
	@Override
	public SysUser saveUser(SysUser user) {
		return sysUserDao.save(user);
	}

	@Override
	public SysUser findUser(String userId) {
		return sysUserDao.findById(userId).get();
	}

	@Override
	@SystemAopLog(description="删除用户")
	public void deleteUser(String userId) {
		SysUser sysUser = new SysUser();
		sysUser.setUserId(userId);
		sysUserDao.delete(sysUser);
	}

	@Override
	public Page<SysUser> pageList(int pageNumber,int pageSize,String sortColom,final SysUser user) throws SocketException{
		Pageable pageable = new PageRequest(pageNumber-1, pageSize, Sort.Direction.ASC,sortColom); 
		Page<SysUser> sysUser = sysUserDao.findAll(new Specification<SysUser>(){  
	            @Override  
	            public Predicate toPredicate(Root<SysUser> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {  
	            	 List<Predicate> list = new ArrayList<Predicate>();  
	                 if(StringUtils.isNotBlank(user.getUserName())){  
	                     list.add(criteriaBuilder.like(root.get("userName").as(String.class), "%" +user.getUserName()+"%"));  
	                 }  
	                 if(null!=user.getCreateTime() && !"".equals(user.getCreateTime())){  
	                     list.add(criteriaBuilder.like(root.get("createTime").as(String.class),"%"+ user.getCreateTime()+"%"));  
	                 }  
//	                 if(StringUtils.isNotBlank(user.DE)){  
//	                	 Path<String> org=root.get("parentMenu").get("name");  
//                         list.add(criteriaBuilder.like(org,"%"+menu.getpName()+"%"));
//	                 }  
	                 list.add(criteriaBuilder.equal(root.get("isVisual").as(String.class),"1"));  
	                 Predicate[] p = new Predicate[list.size()];  
	                 return criteriaBuilder.and(list.toArray(p));
	            }  
	        },pageable); 
		 return sysUser;
	}

	@Override
	public SysUser findByUserName(String userName) {
		return sysUserDao.findByUserName(userName);
	}

	@Override
	public SysUser findUser(String userName, String password) {
		return sysUserDao.findByUserNameAndPassword(userName,password);
	}

}
